package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.*;
import com.xjc.business.entity.po.classadmin.ClassAdminExChangeDormitoryApplyOpera;
import com.xjc.business.entity.po.stu.ExchangeDormitoryApplyPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.classadmin.ClassExchangeDormitoryApplyVo;
import com.xjc.business.entity.vo.coladmin.ColExchangeDormitoryApplyVo;
import com.xjc.business.entity.vo.stu.CurrentSureDormitoryVo;
import com.xjc.business.entity.vo.stu.ExChangeDormitoryApplyVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.StudentChangeBedApplysMapper;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【student_change_bed_applys(学生换宿申请表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:32
*/
@Service
public class StudentChangeBedApplysServiceImpl extends ServiceImpl<StudentChangeBedApplysMapper, StudentChangeBedApplys>
    implements StudentChangeBedApplysService {

    @Autowired
    @Lazy
    WxUserService wxUserService;

    @Autowired
    @Lazy
    StudentsService studentsService;

    @Autowired
    @Lazy
    AllStudentStayRelationsService allStudentStayRelationsService;

    @Autowired
    @Lazy
    AllStudentRegsiterRelationsService allStudentRegsiterRelationsService;

    @Autowired
    @Lazy
    ClassAdminRelationsService classAdminRelationsService;

    @Autowired
    @Lazy
    ClassAdminsService classAdminsService;

    @Autowired
    @Lazy
    BuildingsService buildingsService;

    @Autowired
    @Lazy
    DormitoriesService dormitoriesService;

    @Autowired
    @Lazy
    BedsService bedsService;

    @Autowired
    @Lazy
    CollageAdminRelationsService collageAdminRelationsService;

    /**
     * 获取当前需要审批的他人换宿申请信息
     * @param id 学生微信id
     * @return 宿舍信息
     */
    @Override
    public Result<CurrentSureDormitoryVo> getCurrentSureDormitory(Integer id) {

        Integer stuId = wxUserService.getById(id).getStuId();

        LambdaQueryWrapper<StudentChangeBedApplys> studentChangeBedApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentChangeBedApplysLambdaQueryWrapper.eq(StudentChangeBedApplys::getDestId,stuId);
        studentChangeBedApplysLambdaQueryWrapper.in(StudentChangeBedApplys::getStatus,0,2);
        StudentChangeBedApplys studentChangeBedApplys = getOne(studentChangeBedApplysLambdaQueryWrapper);
        if(studentChangeBedApplys == null){
            return Result.okWithData(null);
        }

        CurrentSureDormitoryVo currentSureDormitoryVo = new CurrentSureDormitoryVo();
        currentSureDormitoryVo.setId(studentChangeBedApplys.getId());
        currentSureDormitoryVo.setSrcBuildId(studentChangeBedApplys.getDestBuildId());
        currentSureDormitoryVo.setSrcBuildName(buildingsService.getById(studentChangeBedApplys.getDestBuildId()).getBno()+"号楼");
        currentSureDormitoryVo.setSrcDormId(studentChangeBedApplys.getDestDormId());
        currentSureDormitoryVo.setSrcDormName(dormitoriesService.getById(studentChangeBedApplys.getDestDormId()).getDno()+"宿舍");
        currentSureDormitoryVo.setSrcBedId(studentChangeBedApplys.getDestBedId());
        currentSureDormitoryVo.setSrcBedName(bedsService.getById(studentChangeBedApplys.getDestBedId()).getBno()+"号床");
        currentSureDormitoryVo.setDestBuildId(studentChangeBedApplys.getSrcBuildId());
        currentSureDormitoryVo.setDestBuildName(buildingsService.getById(studentChangeBedApplys.getSrcBuildId()).getBno()+"号楼");
        currentSureDormitoryVo.setDestDormId(studentChangeBedApplys.getSrcDormId());
        currentSureDormitoryVo.setDestDormName(dormitoriesService.getById(studentChangeBedApplys.getSrcDormId()).getDno()+"宿舍");
        currentSureDormitoryVo.setDestBedId(studentChangeBedApplys.getSrcBedId());
        currentSureDormitoryVo.setDestBedName(bedsService.getById(studentChangeBedApplys.getSrcBedId()).getBno()+"号床");

        currentSureDormitoryVo.setApplyName(studentsService.getById(studentChangeBedApplys.getApplyId()).getName());

        currentSureDormitoryVo.setDestStatus(studentChangeBedApplys.getDestStatus());
        currentSureDormitoryVo.setReason(studentChangeBedApplys.getReason());
        currentSureDormitoryVo.setClassAdminAuditorId(studentChangeBedApplys.getClassAdminAuditorId());
        currentSureDormitoryVo.setCollegeAdminAuditorId(studentChangeBedApplys.getCollegeAdminAuditorId());
        currentSureDormitoryVo.setClassAdminStatus(studentChangeBedApplys.getClassAdminStatus());
        currentSureDormitoryVo.setCollegeAdminStatus(studentChangeBedApplys.getCollegeAdminStatus());
        currentSureDormitoryVo.setStatus(studentChangeBedApplys.getStatus());
        currentSureDormitoryVo.setClassAdminInfo(studentChangeBedApplys.getClassAdminInfo());
        currentSureDormitoryVo.setCollegeAdminInfo(studentChangeBedApplys.getCollegeAdminInfo());
        currentSureDormitoryVo.setCreateTime(studentChangeBedApplys.getCreateTime());

        return Result.okWithData(currentSureDormitoryVo);
    }

    /**
     * 获取当前换宿申请信息
     * @param id 学生微信id
     * @return 宿舍信息
     */
    @Override
    public Result<ExChangeDormitoryApplyVo> getExChangeDormitoryApply(Integer id) {
        Integer stuId = wxUserService.getById(id).getStuId();

        LambdaQueryWrapper<StudentChangeBedApplys> studentChangeBedApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentChangeBedApplysLambdaQueryWrapper.eq(StudentChangeBedApplys::getApplyId,stuId);
        studentChangeBedApplysLambdaQueryWrapper.in(StudentChangeBedApplys::getStatus,0,2);
        StudentChangeBedApplys studentChangeBedApplys = getOne(studentChangeBedApplysLambdaQueryWrapper);
        if(studentChangeBedApplys == null){
            return Result.okWithData(null);
        }

        ExChangeDormitoryApplyVo exChangeDormitoryApplyVo = new ExChangeDormitoryApplyVo();
        exChangeDormitoryApplyVo.setId(studentChangeBedApplys.getId());
        exChangeDormitoryApplyVo.setSrcBuildId(studentChangeBedApplys.getSrcBuildId());
        exChangeDormitoryApplyVo.setSrcBuildName(buildingsService.getById(studentChangeBedApplys.getSrcBuildId()).getBno()+"楼");
        exChangeDormitoryApplyVo.setSrcDormId(studentChangeBedApplys.getSrcDormId());
        exChangeDormitoryApplyVo.setSrcDormName(dormitoriesService.getById(studentChangeBedApplys.getSrcDormId()).getDno()+"宿舍");
        exChangeDormitoryApplyVo.setSrcBedId(studentChangeBedApplys.getSrcBedId());
        exChangeDormitoryApplyVo.setSrcBedName(bedsService.getById(studentChangeBedApplys.getSrcBedId()).getBno()+"号床");
        exChangeDormitoryApplyVo.setDestBuildId(studentChangeBedApplys.getDestBuildId());
        exChangeDormitoryApplyVo.setDestBuildName(buildingsService.getById(studentChangeBedApplys.getDestBuildId()).getBno()+"楼");
        exChangeDormitoryApplyVo.setDestDormId(studentChangeBedApplys.getDestDormId());
        exChangeDormitoryApplyVo.setDestDormName(dormitoriesService.getById(studentChangeBedApplys.getDestDormId()).getDno()+"宿舍");
        exChangeDormitoryApplyVo.setDestBedId(studentChangeBedApplys.getDestBedId());
        exChangeDormitoryApplyVo.setDestBedName(bedsService.getById(studentChangeBedApplys.getDestBedId()).getBno()+"号床");
        Students students = studentsService.getById(studentChangeBedApplys.getDestId());
        exChangeDormitoryApplyVo.setDestName(students == null?"无":students.getName());

        exChangeDormitoryApplyVo.setDestStatus(studentChangeBedApplys.getDestStatus());
        exChangeDormitoryApplyVo.setReason(studentChangeBedApplys.getReason());
        exChangeDormitoryApplyVo.setClassAdminAuditorId(studentChangeBedApplys.getClassAdminAuditorId());
        exChangeDormitoryApplyVo.setCollegeAdminAuditorId(studentChangeBedApplys.getCollegeAdminAuditorId());
        exChangeDormitoryApplyVo.setClassAdminStatus(studentChangeBedApplys.getClassAdminStatus());
        exChangeDormitoryApplyVo.setCollegeAdminStatus(studentChangeBedApplys.getCollegeAdminStatus());
        exChangeDormitoryApplyVo.setStatus(studentChangeBedApplys.getStatus());
        exChangeDormitoryApplyVo.setClassAdminInfo(studentChangeBedApplys.getClassAdminInfo());
        exChangeDormitoryApplyVo.setCollegeAdminInfo(studentChangeBedApplys.getCollegeAdminInfo());
        exChangeDormitoryApplyVo.setCreateTime(studentChangeBedApplys.getCreateTime());

        return Result.okWithData(exChangeDormitoryApplyVo);
    }

    /**
     * 提交换宿申请
     * @param exchangeDormitoryApplyPo 换宿申请信息
     * @return 反馈
     */
    @Override
    @Transactional
    public Result<String> sureExchangeDormitory(ExchangeDormitoryApplyPo exchangeDormitoryApplyPo) {

        Integer wxId = exchangeDormitoryApplyPo.getWxId();
        Integer stuId = wxUserService.getById(wxId).getStuId();

        LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper10 = new LambdaQueryWrapper<>();

        allStudentStayRelationsLambdaQueryWrapper10.eq(AllStudentStayRelations::getStuId,stuId);
        AllStudentStayRelations allStudentStayRelationsFromDB10 = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper10);

        Integer srcBuildId = allStudentStayRelationsFromDB10.getBuildId();
        Integer srcDormId = allStudentStayRelationsFromDB10.getDormId();
        Integer srcBedId = allStudentStayRelationsFromDB10.getBedId();

        Integer destBuildId = exchangeDormitoryApplyPo.getDestBuildId();
        Integer destDormId = exchangeDormitoryApplyPo.getDestDormId();
        Integer destBedId = exchangeDormitoryApplyPo.getDestBedId();

        LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getBuildId,destBuildId);
        allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getDormId,destDormId);
        allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getBedId,destBedId);
        allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getStuId,stuId);
        AllStudentStayRelations allStudentStayRelationsFromDB = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper);
        
        if(allStudentStayRelationsFromDB != null){
            return Result.ok("目标宿舍与当前宿舍重复");
        }

        LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        allStudentStayRelationsLambdaQueryWrapper2.eq(AllStudentStayRelations::getBuildId,destBuildId);
        allStudentStayRelationsLambdaQueryWrapper2.eq(AllStudentStayRelations::getDormId,destDormId);
        allStudentStayRelationsLambdaQueryWrapper2.eq(AllStudentStayRelations::getBedId,destBedId);
        AllStudentStayRelations allStudentStayRelationsFromDB2 = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper);

        StudentChangeBedApplys studentChangeBedApplys = new StudentChangeBedApplys();

        // 目标宿舍无人拥有-无需目标人确认
        if(allStudentStayRelationsFromDB2 == null){

            studentChangeBedApplys.setDestId(-1);
            studentChangeBedApplys.setDestStatus(3);

            LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId,stuId);
            AllStudentRegsiterRelations studentFromDB = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper);
            Integer clazzId = studentFromDB.getClazzId();

            LambdaQueryWrapper<ClassAdminRelations> classAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            classAdminRelationsLambdaQueryWrapper.eq(ClassAdminRelations::getClId,clazzId);
            ClassAdminRelations classAdminRelations = classAdminRelationsService.getOne(classAdminRelationsLambdaQueryWrapper);
            Integer clazzAdminId = classAdminRelations.getClazzAdminId();

            studentChangeBedApplys.setClassAdminAuditorId(clazzAdminId);
            studentChangeBedApplys.setClassAdminStatus(2);

        } else if (allStudentStayRelationsFromDB2.getStatus() == 0) {
            return Result.ok("目标宿舍处于换宿状态，请更换");
        }else if (allStudentStayRelationsFromDB2.getStatus() == 1){
            // 目标宿舍有人拥有且状态为正常-需要目标人确认

            studentChangeBedApplys.setDestId(allStudentStayRelationsFromDB2.getStuId());
            studentChangeBedApplys.setDestStatus(2);

            // 修改目标宿舍状态
            allStudentStayRelationsFromDB2.setStatus(0);
            allStudentStayRelationsService.updateById(allStudentStayRelationsFromDB2);

        }
        studentChangeBedApplys.setReason(exchangeDormitoryApplyPo.getReason());

        studentChangeBedApplys.setSrcBuildId(srcBuildId);
        studentChangeBedApplys.setSrcDormId(srcDormId);
        studentChangeBedApplys.setSrcBedId(srcBedId);
        studentChangeBedApplys.setDestBuildId(exchangeDormitoryApplyPo.getDestBuildId());
        studentChangeBedApplys.setDestDormId(exchangeDormitoryApplyPo.getDestDormId());
        studentChangeBedApplys.setDestBedId(exchangeDormitoryApplyPo.getDestBedId());
        studentChangeBedApplys.setApplyId(stuId);

        studentChangeBedApplys.setStatus(2);
        studentChangeBedApplys.setCreateTime(new Date());
        studentChangeBedApplys.setUpdateTime(new Date());

        save(studentChangeBedApplys);

        // 修改当前宿舍状态
        LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        allStudentStayRelationsLambdaQueryWrapper3.eq(AllStudentStayRelations::getBuildId,srcBuildId);
        allStudentStayRelationsLambdaQueryWrapper3.eq(AllStudentStayRelations::getDormId,srcDormId);
        allStudentStayRelationsLambdaQueryWrapper3.eq(AllStudentStayRelations::getBedId,srcBedId);
        allStudentStayRelationsLambdaQueryWrapper3.eq(AllStudentStayRelations::getStuId,stuId);
        AllStudentStayRelations allStudentStayRelationsFromDB3 = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper3);
        allStudentStayRelationsFromDB3.setStatus(0);
        allStudentStayRelationsService.updateById(allStudentStayRelationsFromDB3);
        
        return Result.okWithData("申请成功");
    }

    /**
     * 目标宿舍拥有者改变申请换宿状态
     * @param id     申请Id
     * @return 反馈
     */
    @Override
    public Result<String> changeExchangeDormitoryApplyStatus(Integer id) {
        StudentChangeBedApplys studentChangeBedApplys = getById(id);
        Integer applyId = studentChangeBedApplys.getApplyId();

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId,applyId);
        AllStudentRegsiterRelations studentFromDB = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper);
        Integer clazzId = studentFromDB.getClazzId();

        LambdaQueryWrapper<ClassAdminRelations> classAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classAdminRelationsLambdaQueryWrapper.eq(ClassAdminRelations::getClId,clazzId);
        ClassAdminRelations classAdminRelations = classAdminRelationsService.getOne(classAdminRelationsLambdaQueryWrapper);
        Integer clazzAdminId = classAdminRelations.getClazzAdminId();

        studentChangeBedApplys.setClassAdminAuditorId(clazzAdminId);
        studentChangeBedApplys.setClassAdminStatus(2);
        studentChangeBedApplys.setDestStatus(1);

        updateById(studentChangeBedApplys);

        return Result.okWithData("同意成功");
    }

    /**
     * 获取换宿申请列表
     * @param pageSize     每页显示条数
     * @param currentPage  当前页
     * @param classAdminId 辅导员id
     * @param status       状态
     * @return Result
     */
    @Override
    public Result<PageVo<ClassExchangeDormitoryApplyVo>> getExchangeDormitoryApplyList(Integer pageSize, Integer currentPage, Integer classAdminId, Integer status) {

        LambdaQueryWrapper<ClassAdminRelations> classAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classAdminRelationsLambdaQueryWrapper.eq(ClassAdminRelations::getClazzAdminId,classAdminId);
        List<Integer> classIdList = classAdminRelationsService.list(classAdminRelationsLambdaQueryWrapper).stream().map(ClassAdminRelations::getClId).collect(Collectors.toList());

        if(classIdList.isEmpty()){
            return Result.okWithData(new PageVo<>());
        }

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.in(AllStudentRegsiterRelations::getClazzId,classIdList);
        List<Integer> stuIdList = allStudentRegsiterRelationsService.list(allStudentRegsiterRelationsLambdaQueryWrapper).stream().map(AllStudentRegsiterRelations::getStuId).collect(Collectors.toList());

        if(stuIdList.isEmpty()){
            return Result.okWithData(new PageVo<>());
        }

        LambdaQueryWrapper<StudentChangeBedApplys> studentChangeBedApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentChangeBedApplysLambdaQueryWrapper.in(StudentChangeBedApplys::getApplyId,stuIdList);
        studentChangeBedApplysLambdaQueryWrapper.in(StudentChangeBedApplys::getDestStatus,1,3);
        studentChangeBedApplysLambdaQueryWrapper.eq(status!=-1,StudentChangeBedApplys::getClassAdminStatus,status);

        Page<StudentChangeBedApplys> studentChangeBedApplysPage = new Page<>(currentPage,pageSize);
        page(studentChangeBedApplysPage,studentChangeBedApplysLambdaQueryWrapper);
        List<StudentChangeBedApplys> studentChangeBedApplysPageRecords = studentChangeBedApplysPage.getRecords();
        List<ClassExchangeDormitoryApplyVo> classExchangeDormitoryApplyVoList = studentChangeBedApplysPageRecords.stream().map(studentChangeBedApplys -> {
            ClassExchangeDormitoryApplyVo classExchangeDormitoryApplyVo = new ClassExchangeDormitoryApplyVo();
            classExchangeDormitoryApplyVo.setId(studentChangeBedApplys.getId());
            classExchangeDormitoryApplyVo.setSrcBuildId(studentChangeBedApplys.getSrcBuildId());
            classExchangeDormitoryApplyVo.setSrcBuildName(buildingsService.getById(studentChangeBedApplys.getSrcBuildId()).getBno()+"号楼");
            classExchangeDormitoryApplyVo.setSrcDormId(studentChangeBedApplys.getSrcDormId());
            classExchangeDormitoryApplyVo.setSrcDormName(dormitoriesService.getById(studentChangeBedApplys.getSrcDormId()).getDno()+"");
            classExchangeDormitoryApplyVo.setSrcBedId(studentChangeBedApplys.getSrcBedId());
            classExchangeDormitoryApplyVo.setSrcBedName(bedsService.getById(studentChangeBedApplys.getSrcBedId()).getBno()+"号");
            classExchangeDormitoryApplyVo.setDestBuildId(studentChangeBedApplys.getDestBuildId());
            classExchangeDormitoryApplyVo.setDestBuildName(buildingsService.getById(studentChangeBedApplys.getDestBuildId()).getBno()+"号楼");
            classExchangeDormitoryApplyVo.setDestDormId(studentChangeBedApplys.getDestDormId());
            classExchangeDormitoryApplyVo.setDestDormName(dormitoriesService.getById(studentChangeBedApplys.getDestDormId()).getDno()+"");
            classExchangeDormitoryApplyVo.setDestBedId(studentChangeBedApplys.getDestBedId());
            classExchangeDormitoryApplyVo.setDestBedName(bedsService.getById(studentChangeBedApplys.getDestBedId()).getBno()+"号");
            classExchangeDormitoryApplyVo.setApplyName(studentsService.getById(studentChangeBedApplys.getApplyId()).getName());
            if(studentChangeBedApplys.getDestStatus() != 3){
                classExchangeDormitoryApplyVo.setDestName(studentsService.getById(studentChangeBedApplys.getDestId()).getName());
            }else{
                classExchangeDormitoryApplyVo.setDestName("无");
            }
            classExchangeDormitoryApplyVo.setDestStatus(studentChangeBedApplys.getDestStatus());
            classExchangeDormitoryApplyVo.setReason(studentChangeBedApplys.getReason());
            classExchangeDormitoryApplyVo.setClassAdminStatus(studentChangeBedApplys.getClassAdminStatus());
            classExchangeDormitoryApplyVo.setCollegeAdminStatus(studentChangeBedApplys.getCollegeAdminStatus());
            classExchangeDormitoryApplyVo.setStatus(studentChangeBedApplys.getStatus());
            classExchangeDormitoryApplyVo.setClassAdminInfo(studentChangeBedApplys.getClassAdminInfo());
            classExchangeDormitoryApplyVo.setCollegeAdminInfo(studentChangeBedApplys.getCollegeAdminInfo());
            classExchangeDormitoryApplyVo.setCreateTime(studentChangeBedApplys.getCreateTime());

            return classExchangeDormitoryApplyVo;
        }).collect(Collectors.toList());

        PageVo<ClassExchangeDormitoryApplyVo> pageVo = new PageVo<>();
        pageVo.setPageCount(studentChangeBedApplysPage.getPages());
        pageVo.setSize(pageSize);
        pageVo.setTotal(studentChangeBedApplysPage.getTotal());
        pageVo.setData(classExchangeDormitoryApplyVoList);
        pageVo.setCurrent(currentPage);

        return Result.okWithData(pageVo);
    }

    /**
     * 获取换宿申请列表
     *
     * @param pageSize       每页显示条数
     * @param currentPage    当前页
     * @param collegeAdminId 辅导员id
     * @param status         状态
     * @return Result
     */
    @Override
    public Result<PageVo<ColExchangeDormitoryApplyVo>> getExchangeDormitoryApplyListWithCollege(Integer pageSize, Integer currentPage, Integer collegeAdminId, Integer status) {
        LambdaQueryWrapper<CollageAdminRelations> collageAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageAdminRelationsLambdaQueryWrapper.eq(CollageAdminRelations::getColAdminId,collegeAdminId);
        Integer colId = collageAdminRelationsService.getOne(collageAdminRelationsLambdaQueryWrapper).getColId();

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getColId,colId);
        List<Integer> stuIdList = allStudentRegsiterRelationsService.list(allStudentRegsiterRelationsLambdaQueryWrapper).stream().map(AllStudentRegsiterRelations::getStuId).collect(Collectors.toList());

        if(stuIdList.isEmpty()){
            return Result.okWithData(new PageVo<>());
        }

        LambdaQueryWrapper<StudentChangeBedApplys> studentChangeBedApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentChangeBedApplysLambdaQueryWrapper.in(StudentChangeBedApplys::getApplyId,stuIdList);
        studentChangeBedApplysLambdaQueryWrapper.in(StudentChangeBedApplys::getDestStatus,1,3);
        studentChangeBedApplysLambdaQueryWrapper.eq(StudentChangeBedApplys::getClassAdminStatus,1);

        Page<StudentChangeBedApplys> studentChangeBedApplysPage = new Page<>(currentPage,pageSize);
        page(studentChangeBedApplysPage,studentChangeBedApplysLambdaQueryWrapper);
        List<StudentChangeBedApplys> studentChangeBedApplysPageRecords = studentChangeBedApplysPage.getRecords();
        List<ColExchangeDormitoryApplyVo> classExchangeDormitoryApplyVoList = studentChangeBedApplysPageRecords.stream().map(studentChangeBedApplys -> {
            ColExchangeDormitoryApplyVo colExchangeDormitoryApplyVo = new ColExchangeDormitoryApplyVo();
            colExchangeDormitoryApplyVo.setId(studentChangeBedApplys.getId());
            colExchangeDormitoryApplyVo.setSrcBuildId(studentChangeBedApplys.getSrcBuildId());
            colExchangeDormitoryApplyVo.setSrcBuildName(buildingsService.getById(studentChangeBedApplys.getSrcBuildId()).getBno()+"号楼");
            colExchangeDormitoryApplyVo.setSrcDormId(studentChangeBedApplys.getSrcDormId());
            colExchangeDormitoryApplyVo.setSrcDormName(dormitoriesService.getById(studentChangeBedApplys.getSrcDormId()).getDno()+"");
            colExchangeDormitoryApplyVo.setSrcBedId(studentChangeBedApplys.getSrcBedId());
            colExchangeDormitoryApplyVo.setSrcBedName(bedsService.getById(studentChangeBedApplys.getSrcBedId()).getBno()+"号");
            colExchangeDormitoryApplyVo.setDestBuildId(studentChangeBedApplys.getDestBuildId());
            colExchangeDormitoryApplyVo.setDestBuildName(buildingsService.getById(studentChangeBedApplys.getDestBuildId()).getBno()+"号楼");
            colExchangeDormitoryApplyVo.setDestDormId(studentChangeBedApplys.getDestDormId());
            colExchangeDormitoryApplyVo.setDestDormName(dormitoriesService.getById(studentChangeBedApplys.getDestDormId()).getDno()+"");
            colExchangeDormitoryApplyVo.setDestBedId(studentChangeBedApplys.getDestBedId());
            colExchangeDormitoryApplyVo.setDestBedName(bedsService.getById(studentChangeBedApplys.getDestBedId()).getBno()+"号");
            colExchangeDormitoryApplyVo.setApplyName(studentsService.getById(studentChangeBedApplys.getApplyId()).getName());
            if(studentChangeBedApplys.getDestStatus() != 3){
                colExchangeDormitoryApplyVo.setDestName(studentsService.getById(studentChangeBedApplys.getDestId()).getName());
            }else{
                colExchangeDormitoryApplyVo.setDestName("无");
            }
            colExchangeDormitoryApplyVo.setDestStatus(studentChangeBedApplys.getDestStatus());
            colExchangeDormitoryApplyVo.setReason(studentChangeBedApplys.getReason());
            colExchangeDormitoryApplyVo.setClassAdminStatus(studentChangeBedApplys.getClassAdminStatus());
            colExchangeDormitoryApplyVo.setCollegeAdminStatus(studentChangeBedApplys.getCollegeAdminStatus());
            colExchangeDormitoryApplyVo.setStatus(studentChangeBedApplys.getStatus());
            colExchangeDormitoryApplyVo.setClassAdminInfo(studentChangeBedApplys.getClassAdminInfo());
            colExchangeDormitoryApplyVo.setCollegeAdminInfo(studentChangeBedApplys.getCollegeAdminInfo());
            colExchangeDormitoryApplyVo.setCreateTime(studentChangeBedApplys.getCreateTime());

            return colExchangeDormitoryApplyVo;
        }).collect(Collectors.toList());

        PageVo<ColExchangeDormitoryApplyVo> pageVo = new PageVo<>();
        pageVo.setPageCount(studentChangeBedApplysPage.getPages());
        pageVo.setSize(pageSize);
        pageVo.setTotal(studentChangeBedApplysPage.getTotal());
        pageVo.setData(classExchangeDormitoryApplyVoList);
        pageVo.setCurrent(currentPage);

        return Result.okWithData(pageVo);
    }

    /**
     * 学院管理员审批换宿申请
     * @param classAdminExChangeDormitoryApplyOpera 审批意见
     * @return Result
     */
    @Override
    @Transactional
    public Result<String> changeDormitoryApplyOpera2(ClassAdminExChangeDormitoryApplyOpera classAdminExChangeDormitoryApplyOpera) {
        Integer id = classAdminExChangeDormitoryApplyOpera.getId();
        Integer classAdminStatus = classAdminExChangeDormitoryApplyOpera.getClassAdminStatus();
        String classAdminInfo = classAdminExChangeDormitoryApplyOpera.getClassAdminInfo();

        // 更新申请状态
        StudentChangeBedApplys studentChangeBedApplys = getById(id);
        studentChangeBedApplys.setCollegeAdminInfo(classAdminInfo);
        studentChangeBedApplys.setCollegeAdminStatus(classAdminStatus);
        studentChangeBedApplys.setStatus(1);
        updateById(studentChangeBedApplys);

        // 更新申请人宿舍绑定信息
        Integer applyId = studentChangeBedApplys.getApplyId();
        LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getStuId,applyId);
        AllStudentStayRelations allStudentStayRelations = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper);
        allStudentStayRelations.setBuildId(studentChangeBedApplys.getDestBuildId());
        allStudentStayRelations.setDormId(studentChangeBedApplys.getDestDormId());
        allStudentStayRelations.setBedId(studentChangeBedApplys.getDestBedId());
        allStudentStayRelations.setStatus(1);
        allStudentStayRelationsService.updateById(allStudentStayRelations);


        // 更新对方宿舍绑定信息
        Integer destId = studentChangeBedApplys.getDestId();
        if(destId!=-1){
            LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            allStudentStayRelationsLambdaQueryWrapper2.eq(AllStudentStayRelations::getStuId,destId);
            AllStudentStayRelations allStudentStayRelations2 = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper2);
            allStudentStayRelations2.setBuildId(studentChangeBedApplys.getSrcBuildId());
            allStudentStayRelations2.setDormId(studentChangeBedApplys.getSrcDormId());
            allStudentStayRelations2.setBedId(studentChangeBedApplys.getSrcBedId());
            allStudentStayRelations2.setStatus(1);
            allStudentStayRelationsService.updateById(allStudentStayRelations2);
        }else{
            if(studentChangeBedApplys.getSrcBuildId()!=studentChangeBedApplys.getDestBuildId()){
                // 目标宿舍楼和原宿舍楼不是同一个 且 对方宿舍无人拥有
                // 更新目标宿舍楼和原宿舍楼人数
                Buildings destBuilding = buildingsService.getById(destId);
                destBuilding.setHasLiving(destBuilding.getHasLiving()+1);
                destBuilding.setNotLiving(destBuilding.getNotLiving()-1);
                buildingsService.updateById(destBuilding);

                Buildings srcBuilding = buildingsService.getById(studentChangeBedApplys.getSrcBuildId());
                srcBuilding.setHasLiving(srcBuilding.getHasLiving()-1);
                srcBuilding.setNotLiving(srcBuilding.getNotLiving()+1);
                buildingsService.updateById(srcBuilding);
            }

        }

        return Result.okWithData("审核成功");
    }

    /**
     * 辅导员审批换宿申请
     *
     * @param classAdminExChangeDormitoryApplyOpera 审批意见
     * @return Result
     */
    @Override
    public Result<String> changeDormitoryApplyOpera(ClassAdminExChangeDormitoryApplyOpera classAdminExChangeDormitoryApplyOpera) {
        Integer id = classAdminExChangeDormitoryApplyOpera.getId();
        Integer classAdminStatus = classAdminExChangeDormitoryApplyOpera.getClassAdminStatus();
        String classAdminInfo = classAdminExChangeDormitoryApplyOpera.getClassAdminInfo();

        // 更新申请状态
        StudentChangeBedApplys studentChangeBedApplys = getById(id);
        studentChangeBedApplys.setClassAdminInfo(classAdminInfo);
        studentChangeBedApplys.setClassAdminStatus(classAdminStatus);
        updateById(studentChangeBedApplys);

        return Result.okWithData("审核成功");
    }
}